package com.changgou.goods.controller;

import com.changgou.goods.pojo.Category;
import com.changgou.goods.service.CategoryService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /****
     * 查询所有产品分类
     * @return
     */
    @GetMapping
    public Result<List<Category>> findAll() {
        return new Result<>(true, StatusCode.OK, "查询成功", categoryService.findAll());
    }

    /****
     * 添加产品分类
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Category category) {
        categoryService.add(category);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    /****
     * 根据父id查询子分类
     * @param pid
     * @return
     */
    @RequestMapping("/list/{pid}")
    public Result<List<Category>> findByParentId(@PathVariable("pid") Integer pid) {
        return new Result<List<Category>>(true, StatusCode.OK, "查询成功", categoryService.findByParentId(pid));
    }

    /****
     * 根据条件查询
     * @param category
     * @return
     */
    @PostMapping("/search")
    public Result<List<Category>> findList(@RequestBody(required = false) Category category) {
        return new Result<List<Category>>(true, StatusCode.OK, "查询成功", categoryService.findList(category));
    }

    /****
     * 分页查询所有
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo<Category>> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        return new Result<>(true, StatusCode.OK, "查询成功", categoryService.findPage(page, size));
    }

    /****
     * 根据条件分页查询
     * @param page
     * @param size
     * @param category
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo<Category>> findPage(@PathVariable Integer page, @PathVariable Integer size,
                                               @RequestBody(required = false) Category category) {
        return new Result<>(true, StatusCode.OK, "查询成功", categoryService.findPage(page, size, category));
    }

    /****
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable Integer id) {
        Category category = categoryService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", category);
    }

    /****
     * 根据Id修改
     * @param id
     * @param category
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Category category) {
        category.setId(id);
        categoryService.update(category);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /****
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        categoryService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }
}
